import datetime
import time
from repeat import Repeat

try: #this code block from pyXSD (http://pyxsd.org)
    import lxml.etree as ET #lxml uses libxml2, which is extra quick
except:
    try:    # Thanks to the ElementTree library (http://www.effbot.org/zone/element-index.htm)
        import cElementTree as ET
    except:
        try:
            from xml.etree import cElementTree as ET
        except:
            try:
                from xml.etree import ElementTree as ET
            except:
                try:
                    import elementtree.ElementTree as ET
                except:
                    raise ImportError, "Your system needs the ElementTree or cElementTree library in order to run this package"


class Event(object):
    def __init__(self, name, startDatetime, endDatetime, description, location, allDay, repeatFreq = repeat(0)):
        self.name          = name
        self.startDatetime = startDatetime #datetime
        self.endDatetime   = endDatetime #datetime
        self.description   = description
        self.location      = location
        self.repeatFreq    = repeatFreq
        self.allDay        = allDay #Boolean

    def buildVEvent(self):
        pass

    def buildCsvEvent(self):
        ##"Subject", "Start Date", "Start Time", "Description", "End Date", "End Time", "All day event"
        ##"Meeting in Detroit","26-07-04","00:00:01","Meeting in Detroit","26-07-04","23:59:59","true"
        csvString = '"' + self.name + '",'
        csvString+= '"' + self.startDatetime.strftime("%m-%d-%y") + '",'
        csvString+= '"' + self.startDatetime.strftime("%H:%M:%S") + '",'
        csvString+= '"' + self.description + '",'
        csvString+= '"' + self.endDatetime.strftime("%m-%d-%y") + '",'
        csvString+= '"' + self.endDatetime.strftime("%H:%M:%S") + '",'
        if self.allDay:
            csvString+= '"' + "true" + '"'
        else:
            csvString+= '"' + "false" + '"'
        return csvString

    def buildXML(self, parentElem):
        eventElem = ET.SubElement(parentElem, "event")
        eventElem.set("name", self.name)
        eventElem.set("allDay", str(self.allDay))
        eventElem.set("startDatetime", self.startDatetime.isoformat())
        eventElem.set("endDatetime", self.endDatetime.isoformat())
        eventElem.set("repeatFreq", str(self.repeatFreq))
        locationElem         = ET.SubElement(eventElem, "location")
        locationElem.text    = self.location
        descriptionElem      = ET.SubElement(eventElem, "description")
        descriptionElem.text = self.description
        

    def makeEventFromXML(cls, xmlTag):
        return cls(xmlTag.get("name"), datetime.datetime.fromtimestamp(xmlTag.get("startDatetime")),
                   datetime.datetime.fromtimestamp(xmlTag.get("endDatetime")),
                   xmlTag.find("description").text, xmlTag.find("location").text,
                   Repeat.makeRepeatFromString(xmlTag.get("repeatFreq")), bool(xmlTag.get("allDay")))
    makeEventFromXML = classmethod(makeEventFromXML)
	
    
